package com.coderman.databole.service;

import com.alibaba.fastjson.JSON;
import com.coderman.daoclient.dao.impl.IDaoImpl;
import com.coderman.daoclient.utils.DateBuilder;
import com.coderman.databole.model.entity.BoleBlogEntity;
import com.coderman.databole.model.entity.BoleUserEntity;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import us.codecraft.xsoup.Xsoup;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by 58-daojia on 2017-12-27.
 *
 * @author: fanchunshuai
 * @version: V1.0
 * @Desc:
 * @Copyright (c) 2017 58到家-版权所有
 */
public class ReadBlogToDBService {
    private static IDaoImpl<BoleUserEntity,Long> userEntityIDao = new IDaoImpl<>();
    private static IDaoImpl<BoleBlogEntity,Long> blogEntityLongIDao = new IDaoImpl<>();

    public void readBlog2DB(String filePath) throws IOException {
        File file = new File(filePath);
        BoleBlogEntity boleBlogEntity = new BoleBlogEntity();
        
        Document document = null;
        String fileName = file.getName().replace(".html","");

        document = Jsoup.parse(file, "UTF-8");

        String authorTag = "*//*//**//*[@id='post-$']/div[3]/div[1]".replace("$",fileName);


        //获取博客标题
        String titleTag = boleBlogEntity.getXpathMap().get("blogTitle").replace("$",fileName);
        String titleName = Xsoup.compile(titleTag).evaluate(document).get();
        boleBlogEntity.setBlogTitle(titleName);

        //发布日期
        String publishDateTag = "//*[@id=\"post-"+fileName+"\"]/div[2]/p/text()";
        String publishDate = Xsoup.compile(publishDateTag).evaluate(document).get();
        if(publishDate != null){
            publishDate = publishDate.substring(0,10).replace("/","-");
            boleBlogEntity.setPublishDate( DateBuilder.convertStringToDate(publishDate,"yyyy-MM-dd"));
        }

        //博客分类
        String blogCategoryTag = "//*[@id=\"post-"+fileName+"\"]/div[2]/p/a[1]/text()";
        String blogCategory = Xsoup.compile(blogCategoryTag).evaluate(document).get();
        boleBlogEntity.setBlogCategory(blogCategory);
        StringBuilder builder = new StringBuilder();

        //博客标签
        String publishBlogTag = "//*[@id=\"post-"+fileName+"\"]/div[2]/p/a[3]/text()";
        String publishBlog1 = Xsoup.compile(publishBlogTag).evaluate(document).get();
        if(publishBlog1 != null && !publishBlog1.contains("条评论")){
            builder.append(publishBlog1);
            builder.append(",");
        }

        //博客标签
        String publishBlogTag2 = "//*[@id=\"post-"+fileName+"\"]/div[2]/p/a[4]/text()";
        String publishBlog2 = Xsoup.compile(publishBlogTag2).evaluate(document).get();
        if(publishBlog2 != null){
            builder.append(publishBlog2);
            builder.append(",");
        }

        //博客标签
        String publishBlogTag3 = "//*[@id=\"post-"+fileName+"\"]/div[2]/p/a[5]/text()";
        String publishBlog3 = Xsoup.compile(publishBlogTag3).evaluate(document).get();

        if(publishBlog3 != null){
            builder.append(publishBlog3);
            builder.append(",");
        }
        if(builder.length() >0){

            boleBlogEntity.setBlogTags(builder.toString().substring(0,builder.length() - 1));
        }

        //博客评论数量
        String commentTag = "//*[@id=\"post-"+fileName+"\"]/div[2]/p/a[2]/text()";
        String commentNum = Xsoup.compile(commentTag).evaluate(document).get();
        if(commentNum != null && !commentNum.contains("评论")){
            boleBlogEntity.setBlogTags(boleBlogEntity.getBlogTags()+","+commentNum);
        }


        if(commentNum != null && commentNum.contains("评论")){
            commentNum = commentNum.replace(" 条评论","");
        }

        if(StringUtils.isNumeric(commentNum)){
            boleBlogEntity.setCommentNum(Integer.parseInt(commentNum));
        }else{
            //博客评论数量
            commentTag = "//*[@id=\"post-"+fileName+"\"]/div[2]/p/a[3]/text()";
            String commentStr = Xsoup.compile(commentTag).evaluate(document).get();
            if(commentStr!= null && !StringUtils.isNumeric(commentNum)){
                boleBlogEntity.setBlogTags(boleBlogEntity.getBlogTags()+","+commentNum);
            }else if(commentStr!= null && commentNum.contains("评论")){
                commentNum = commentNum.replace(" 条评论","");
                boleBlogEntity.setCommentNum(Integer.parseInt(commentNum));
            }

            commentTag = "//*[@id=\"post-"+fileName+"\"]/div[2]/p/a[4]/text()";
            commentStr = Xsoup.compile(commentTag).evaluate(document).get();
            if(commentStr!= null && !StringUtils.isNumeric(commentNum)){
                boleBlogEntity.setBlogTags(boleBlogEntity.getBlogTags()+","+commentNum);
            }else if(commentStr!= null && commentNum.contains("评论")){
                commentNum = commentNum.replace(" 条评论","");
                boleBlogEntity.setCommentNum(Integer.parseInt(commentNum));
            }
        }

        if(commentNum != null && !StringUtils.isNumeric(commentNum) ){
            if(commentNum.contains("评论")){
                commentNum = commentNum.trim().replace("评论","").replace(" 条评论","").replace(" ","");
                boleBlogEntity.setCommentNum(Integer.parseInt(commentNum));
            }else{
                boleBlogEntity.setBlogTags(boleBlogEntity.getBlogTags()+","+commentNum);
            }
        }

        if(boleBlogEntity.getBlogTags() != null){
            String [] array = boleBlogEntity.getBlogTags().split(",");
            StringBuilder builder1 = new StringBuilder();

            for (String s : array){
                if(!s.contains("评论") && !"null".equals(s) && StringUtils.isNotBlank(s)){
                    builder1.append(s+",");
                }
            }

            boleBlogEntity.setBlogTags(builder1.toString().substring(0,builder1.length() - 1));
        }


        String simpleLinksTag = "//*[@id=\"wrapper\"]/div[1]/ul//a/@href";
        List<String> simpleLinkList = Xsoup.compile(simpleLinksTag).evaluate(document).list();
        if(simpleLinkList.size() == 0){
            String referLinksTag = "//*[@id=\"wrapper\"]/div[3]/div[2]/ul//a/@href";
            List<String> referLinks = Xsoup.compile(referLinksTag).evaluate(document).list();
            boleBlogEntity.setSimpleLinks(JSON.toJSONString(referLinks));

        }else {
            boleBlogEntity.setSimpleLinks(JSON.toJSONString(simpleLinkList));
        }



        String  authorNameTag = Xsoup.compile(authorTag).evaluate(document).get();


        if(authorNameTag.contains("原文出处")) {

            //博客源地址
            String sourceLinkTag = boleBlogEntity.getXpathMap().get("sourceLink").replace("$",fileName);
            String sourceLink = Xsoup.compile(sourceLinkTag).evaluate(document).get();
            boleBlogEntity.setSourceLink(sourceLink);

            //博客作者名称
            String authorUserNameTag = boleBlogEntity.getXpathMap().get("authorName").replace("$",fileName);
            String authorUserName = Xsoup.compile(authorUserNameTag).evaluate(document).get();
            boleBlogEntity.setAuthorName(authorUserName);
            boleBlogEntity.setBlogType("转载");

        }else  if(authorNameTag.contains("本文作者")){
            String authorUserNameTag = "//*[@id='post-"+fileName+"']/div[3]/div[1]/a[2]/text()";
            String authorUserName = Xsoup.compile(authorUserNameTag).evaluate(document).get();

            boleBlogEntity.setAuthorName(authorUserName);
            String boleUserNameTag = "//*[@id='post-"+fileName+"']/div[3]/div[1]/a[2]/@href";
            String boleUserName = Xsoup.compile(boleUserNameTag).evaluate(document).get();
            if(boleUserName != null && boleUserName.contains("www.importnew.com")){
                boleBlogEntity.setBoleUserName(boleUserName.replace("http://www.importnew.com/author/",""));
            }

            if(boleUserName != null && boleUserName.contains("www.jobbole.com")){
                boleBlogEntity.setBoleUserName(boleUserName.replace("http://www.jobbole.com/members/",""));
            }
            boleBlogEntity.setBlogType("原创");

        }else if(authorNameTag.contains("翻译自")){
            boleBlogEntity.setBlogType("翻译");
            String boleUserNameTag = "//*[@id='post-"+fileName+"']/div[3]/div[1]/a[2]/@href";
            String boleUserName = Xsoup.compile(boleUserNameTag).evaluate(document).get();
            if(boleUserName != null && boleUserName.contains("www.importnew.com")){
                boleBlogEntity.setBoleUserName(boleUserName.replace("http://www.importnew.com/author/",""));
            }

            if(boleUserName != null && boleUserName.contains("www.jobbole.com")){
                boleBlogEntity.setBoleUserName(boleUserName.replace("http://www.jobbole.com/members/",""));
            }

            String sourceLinkTag = "//*[@id='post-"+fileName+"']/div[3]/div[1]/a[3]/@href";
            //博客源地址
            String sourceLink = Xsoup.compile(sourceLinkTag).evaluate(document).get();
            boleBlogEntity.setSourceLink(sourceLink);


        }else if(authorNameTag.contains("翻译组")){
            boleBlogEntity.setBlogType("翻译");
            String boleUserNameTag = "//*[@id='post-"+fileName+"']/div[3]/div[1]/a[2]/@href";
            String boleUserName = Xsoup.compile(boleUserNameTag).evaluate(document).get();
            //设置伯乐博客用户
            if(boleUserName != null && boleUserName.contains("www.importnew.com")){
                boleBlogEntity.setBoleUserName(boleUserName.replace("http://www.importnew.com/author/",""));
            }

            if(boleUserName != null && boleUserName.contains("www.jobbole.com")){
                boleBlogEntity.setBoleUserName(boleUserName.replace("http://www.jobbole.com/members/",""));
            }


            String sourceLinkTag = "//*[@id='post-"+fileName+"']/div[3]/div[1]/a[4]/@href";
            //博客源地址
            String sourceLink = Xsoup.compile(sourceLinkTag).evaluate(document).get();
            boleBlogEntity.setSourceLink(sourceLink);
        }else {
            boleBlogEntity.setBlogType("其他");
        }



        String catLinkTag = "//*[@id=\"breadcrumb\"]/div";
        String catLink = Xsoup.compile(catLinkTag).evaluate(document).get();
        if(catLink != null && catLink.contains("Python")){
            boleBlogEntity.setBlogUrl("http://python.jobbole.com/"+fileName+"/");
        }else{
            boleBlogEntity.setBlogUrl("http://www.importnew.com"+"/"+fileName+".html");
        }

        try {

            if(boleBlogEntity.getBoleUserName() != null){
                List<BoleUserEntity>  boleUserEntityList = userEntityIDao.getList(BoleUserEntity.class,"select * from bole_user where user_name = '"+boleBlogEntity.getBoleUserName()+"'");
                if(!boleUserEntityList.isEmpty()){
                    boleBlogEntity.setUserId(boleUserEntityList.get(0).getId());
                }
            }


            blogEntityLongIDao.insertEntity(boleBlogEntity);



        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    public static void main(String[] args) {
        ReadBlogToDBService blogToDBService = new ReadBlogToDBService();


        File file = new File("E:\\spiderFile\\jobbole\\blog");
        try {
            for (File file1 : file.listFiles()){
                blogToDBService.readBlog2DB(file1.getAbsolutePath());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
